#include "DataCtrl.h"
#include "SqliteProxy.h"
#include <QVariant>

CDataCtrl::CDataCtrl(QObject *parent /*= nullptr*/)
	: QObject(parent)
{
	m_pSqlProxy = new CSqliteProxy(this);
}

CDataCtrl::~CDataCtrl()
{
}

const QMap<QString, CGroupOpt>& CDataCtrl::getGroups()
{
	return m_groups;
}

const QMap<QString, QList<CConnectOpt>>& CDataCtrl::getConnects()
{
	return m_connects;
}

void CDataCtrl::slotInitDbData()
{
	initGroup();
	initConnect();
}

void CDataCtrl::initGroup()
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("select * from \"group\"");
	QList<QMap<QString, QVariant>> results;
	m_pSqlProxy->sqlExeDQL(strSql, results);
	for (int i=0;i<results.count();++i)
	{
		const QMap<QString, QVariant> result = results.at(i);
		CGroupOpt opt;
		opt.strId = result.value("id").toString();
		opt.strName = result.value("name").toString();
		m_groups.insert(opt.strId, opt);
	}
}

void CDataCtrl::initConnect()
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("select * from \"connect\"");
	QList<QMap<QString, QVariant>> results;
	m_pSqlProxy->sqlExeDQL(strSql, results);
	for (int i = 0; i < results.count(); ++i)
	{
		const QMap<QString, QVariant>& result = results.at(i);	
		CConnectOpt opt;
		opt.strUuid = result.value("id").toString();
		opt.strName = result.value("name").toString();
		opt.strClientId = result.value("clientId").toString();
		opt.strAddress = result.value("address").toString();
		opt.port = result.value("port").toInt();
		opt.strUsername = result.value("username").toString();
		opt.strPasswd = result.value("passwd").toString();

		QString strGroupId = result.value("groupId").toString();
		if (m_connects.count(strGroupId) > 0)
		{
			QList<CConnectOpt>& ls = m_connects[strGroupId];
			ls.append(opt);
		}
		else
		{
			QList<CConnectOpt> ls;
			ls.append(opt);
			m_connects.insert(strGroupId, ls);
		}
	}
}

void CDataCtrl::slotInsertGroup(QString strId, QString strName)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("insert into \"group\" values(\"%1\", \"%2\");").arg(strId).arg(strName);
	m_pSqlProxy->sqlExecDML(strSql);
	CGroupOpt opt;
	opt.strId = strId;
	opt.strName = strName;
	m_groups.insert(strId, opt);
}

void CDataCtrl::slotDeleteGroup(QString strId)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("delete from \"connect\" where groupid=\"%1\";").arg(strId);
	m_pSqlProxy->sqlExecDML(strSql);
	m_connects.remove(strId);
	strSql = QString("delete from \"group\" where id=\"%1\";").arg(strId);
	m_pSqlProxy->sqlExecDML(strSql);
	m_groups.remove(strId);
}

void CDataCtrl::slotUpdateGroup(QString strId, QString strName)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("UPDATE \"group\" SET name=\"%1\" WHERE id=\"%2\";").arg(strName).arg(strId);
	m_pSqlProxy->sqlExecDML(strSql);
	CGroupOpt& opt = m_groups[strId];
	opt.strName = strName;
}

void CDataCtrl::slotInsertConnect(QString strGroupId, CConnectOpt opt)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("insert into \"connect\" values(\"%1\", \"%2\", \"%3\", \"%4\", %5, \"%6\", \"%7\", \"%8\", \"%9\", \"%10\");")
		.arg(opt.strUuid).arg(opt.strName)
		.arg(opt.strClientId)
		.arg(opt.strAddress).arg(opt.port)
		.arg(opt.strUsername).arg(opt.strPasswd)
		.arg(opt.time.toString("yyyy-MM-dd hh:mm:ss")).arg(opt.strDescribe)
		.arg(strGroupId);
	m_pSqlProxy->sqlExecDML(strSql);
	if (m_connects.count(strGroupId) > 0)
	{
		QList<CConnectOpt>& opts = m_connects[strGroupId];
		opts.append(opt);
	}
	else
	{
		QList<CConnectOpt> opts;
		opts.append(opt);
		m_connects.insert(strGroupId, opts);
	}
}

void CDataCtrl::slotDeleteConnect(QString strId)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("delete from \"connect\" where id=\"%1\";").arg(strId);
	m_pSqlProxy->sqlExecDML(strSql);
	for (QMap<QString, QList<CConnectOpt>>::iterator it=m_connects.begin();it!=m_connects.end();++it)
	{
		int pos = -1;
		QList<CConnectOpt>& ls = m_connects[it.key()];
		for (int i=0;i<ls.count();++i)
		{
			if (ls.at(i).strUuid == strId)
			{
				pos = i;
				break;
			}
		}
		if (pos > -1)
		{
			ls.removeAt(pos);
			break;
		}
	}
}

void CDataCtrl::slotUpdateConnect(CConnectOpt opt)
{
	if (!m_pSqlProxy)
	{
		return;
	}
	QString strSql = QString("update \"connect\" set name=\"%1\", clientId=\"%1\", address=\"%1\", port=\"%1\", username=\"%1\", passwd=\"%1\", where id=\"%2\";");
	m_pSqlProxy->sqlExecDML(strSql);
	for (QMap<QString, QList<CConnectOpt>>::iterator it = m_connects.begin(); it != m_connects.end(); ++it)
	{
		int pos = -1;
		QList<CConnectOpt>& ls = m_connects[it.key()];
		for (int i = 0; i < ls.count(); ++i)
		{
			if (ls.at(i).strUuid == opt.strUuid)
			{
				pos = i;
				break;
			}
		}
		if (pos > -1)
		{
			CConnectOpt& lsOpt = ls[pos];
			lsOpt = opt;
			break;
		}
	}
}

